const cacheUtil = {
	setCookie : (name,value,{maxAge,domain,path,secure}={})=>{
		let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
		if(typeof maxAge == 'number'){
			cookieText += `; max-age=${maxAge}`
		}
		if(domain){
			cookieText += `; domain=${domain}`
		}
		if(path){
			cookieText += `; path=${path}`
		}
		if(secure){
			cookieText += `; secure=${secure}`
		}
		document.cookie = cookieText
	},
	//获取cookie
	 getCookie : (name)=>{
		if(name){
			const cookieList = document.cookie.split('; ');
			for(let val of cookieList){
				const [cookieName,cookieVal] = [val.split('=')[0],val.split('=')[1]]
				if(name == decodeURIComponent(cookieName)){
					return decodeURIComponent(cookieVal)
				}
			}
		}
		return '';
	},
	
	// 删除cookie
	
	 deleteCookie : (key)=>{
		setCookie(key,'',-1)
	},
	// 设置
	setlocalstroge:(key,val)=>{
		if(key && val){
			localStorage.setItem(key,JSON.stringify(val))
		}
	},
	getlocalstroge:(key)=>{
		if(key){
			const jsonstr = localStorage.getItem(key)
			return JSON.parse(jsonstr)
		}
	}
}

export default cacheUtil